package com.topscore.uke.ui.activities;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.entities.CommentItemBean;
import com.topscore.uke.mvp.contractors.GetMemberCommentsContractor;
import com.topscore.uke.mvp.presenters.GetMemberCommentsPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.widgets.RecyclerAdapterLoadMoreView;
import com.topscore.uke.utils.DisplayImageOptionsManager;
import com.topscore.utillibrary.widget.CustomRatingBar;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CommentsListActivity extends ToolBarCompatActivity implements GetMemberCommentsContractor.View {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private RecyclerAdapter mRecyclerAdapter;

    private GetMemberCommentsContractor.Presenter mPresenter;

    private LoadService mLoadService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments_list);
        ButterKnife.bind(this);
        setCentralTitle("五星好评");
        mPresenter = new GetMemberCommentsPresenter(this);
        initViews();
    }

    private void initViews() {
        mLoadService = LoadSir.getDefault().register(recyclerView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {

            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        mRecyclerAdapter = new RecyclerAdapter();
        recyclerView.setAdapter(mRecyclerAdapter);

        initData();

    }

    private void initData() {
        mPresenter.getComment();
//        List<CommentItemBean> commentList = new ArrayList<>();
//        for (int i = 0; i < 7; i++) {
//            commentList.add(new CommentItemBean());
//        }
//
//        mRecyclerAdapter.setNewData(commentList);
    }

    @Override
    public BaseQuickAdapter getRecyclerAdapter() {
        return mRecyclerAdapter;
    }

    @Override
    public LoadService getLoadService() {
        return mLoadService;
    }

    private class RecyclerAdapter extends BaseQuickAdapter<CommentItemBean,BaseViewHolder>{

        public RecyclerAdapter() {
            super(R.layout.comments_list_item, new ArrayList<>());
            setEmptyView(getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(),false));
            setOnLoadMoreListener(() -> mPresenter.loadNextPage(),recyclerView);
            setEnableLoadMore(true);
            setLoadMoreView(new RecyclerAdapterLoadMoreView());
        }

        @Override
        protected void convert(BaseViewHolder holder, CommentItemBean item) {
            CustomRatingBar ratingBar = holder.getView(R.id.rating_bar);
            ratingBar.setStar(Integer.valueOf(item.getAssessmentLevel()));

            ImageLoader.getInstance().displayImage(item.getMemberIcon(),
                    (ImageView) holder.getView(R.id.member_avatar_iv),
                    DisplayImageOptionsManager.getInstance().getAvatarOptions());

            holder.setText(R.id.comment_content,item.getAssessmentContents())
            .setText(R.id.member_name_tv,item.getMemberName())
            .setText(R.id.member_id_tv,item.getMemberId())
            .setText(R.id.date_tv,item.getAssessmentTime());



        }
    }
}
